import { useAxios } from '@/hooks/useAxios'
import { useMutation, useQuery } from '@tanstack/react-query'
import { AxiosError } from 'axios'
import { toast } from 'sonner'

//获取所有配置（管理员）
export const useGetConfigQuery = (type: 'all' | 'common') => {
  const { axiosInstance } = useAxios()
  return useQuery<Record<string, any>, AxiosError>({
    queryKey: ['useGetConfigQuery', type],
    queryFn: async () => {
      const response = await axiosInstance.get(`/config/${type}`)
      return response.data
    }
  })
}

//保存配置项
export const useSaveConfigMutation = () => {
  const { axiosInstance } = useAxios()
  return useMutation({
    mutationFn: async (data: Record<string, any>) => {
      const response = await axiosInstance.put('/config', data)
      toast(response.data.success)
      return response.data
    }
  })
}
